/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.template;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import net.interknit.knitter.cg.CGMain;
import net.interknit.knitter.cg.model.JavaType;

public class CustomTypeDefTemplateHolder
{
	private static CustomTypeDefTemplateHolder instance;

	public static CustomTypeDefTemplateHolder getInstance()
	{
		return instance != null ? instance : (instance = new CustomTypeDefTemplateHolder());
	}

	private CustomTypeDefTemplate typeDefTemplate = new CustomTypeDefTemplate();
	private final List<String> customTypeDefNames = new Vector<String>();
	private final Map<String, JavaType> customTypeDefs = new Hashtable<String, JavaType>();

	private CustomTypeDefTemplateHolder()
	{
		;
	}

	public CustomTypeDefTemplate getTypeDefTemplate()
	{
		return typeDefTemplate;
	}

	public List<String> getCustomTypeDefNames()
	{
		return customTypeDefNames;
	}

	public Map<String, JavaType> getCustomTypeDefs()
	{
		return customTypeDefs;
	}

	public void addCustomTypeDefinition(JavaType javaType)
	{
		String def = javaType.getJavaTypeId();

		if(! customTypeDefs.containsKey(def))
		{
			if(! javaType.isSeedType() && javaType.getAccessorCount() < 1)
				CGMain.getInstance().log(javaType.getTypeClass() +
						" does not have any accessor (getter/setter) methods");

			customTypeDefNames.add(def);
			customTypeDefs.put(def, javaType);
		}
	}

	public void clear()
	{
		typeDefTemplate = new CustomTypeDefTemplate();
		customTypeDefNames.clear();
		customTypeDefs.clear();
	}
}
